home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / macintosh-c / macc-carbon-demos-nonbinhex.sit / macc-carbon-demos-nonbinhex / chap25-demo-carbon events / Miscellany.c < prev    next >
C/C++ Source or Header  |  2001-05-22  |  10KB  |  330 lines

  1. // *******************************************************************************************
  2. // Miscellany.c
  3. // *******************************************************************************************
  4.  
  5. #include "Miscellany.h"
  6.  
  7. // …………………………………………………………………………………………………………………………………………………………………………………………………… global variables
  8.  
  9. DeviceLoopDrawingUPP    gDeviceLoopDrawUPP;
  10. WindowRef                            gWindowRef;
  11. ControlRef                        gBevelButtonControlRef;
  12. ProcessSerialNumber        gProcessSerNum;
  13. Boolean                                gMultiMonitorsDrawDemo = false;
  14. Boolean                                gColourPickerDemo  = false;
  15. Boolean                                gHelpTagsDemo = false;
  16. RGBColor                            gWhiteColour = { 0xFFFF, 0xFFFF, 0xFFFF };
  17. RGBColor                            gBlueColour  = { 0x6666, 0x6666, 0x9999 };
  18.  
  19. extern Boolean                gNotificationInQueue;
  20.  
  21. // ************************************************************************************** main
  22.  
  23. void  main(void)
  24. {
  25.     MenuBarHandle    menubarHdl;
  26.     SInt32                response;
  27.     MenuRef                menuRef;
  28.     Rect                    contentRect = { 100,100,402,545 };
  29.     Rect                    portRect;
  30.     Rect                    controlRect = { 65,10,155,100 };
  31.     EventTypeSpec    applicationEvents[] =    { { kEventClassApplication, kEventAppActivated    },
  32.                                                                                 { kEventClassCommand,     kEventProcessCommand  } };
  33.     EventTypeSpec    windowEvents[]            = { { kEventClassWindow, kEventWindowDrawContent    },
  34.                                                                                 { kEventClassWindow, kEventWindowGetIdealSize   },
  35.                                                                                 { kEventClassWindow, kEventWindowGetMinimumSize },
  36.                                                                                 { kEventClassWindow, kEventWindowBoundsChanged  } };
  37.  
  38.     // ……………………………………………………………………………………………………………………………………………………………………………………………… do preliminaries
  39.  
  40.     doPreliminaries();
  41.  
  42.     // ……………………………………………………………………………………………………………………………………………… create universal procedure pointer
  43.  
  44.     gDeviceLoopDrawUPP = NewDeviceLoopDrawingUPP((DeviceLoopDrawingProcPtr) deviceLoopDraw);
  45.  
  46.     // ……………………………………………………………………………………………………………………………………………………………………… set up menu bar and menus
  47.  
  48.     menubarHdl = GetNewMBar(rMenubar);
  49.     if(menubarHdl == NULL)
  50.         ExitToShell();
  51.     SetMenuBar(menubarHdl);
  52.     DrawMenuBar();
  53.  
  54.     Gestalt(gestaltMenuMgrAttr,&response);
  55.     if(response & gestaltMenuMgrAquaLayoutMask)
  56.     {
  57.         menuRef = GetMenuRef(mFile);
  58.         if(menuRef != NULL)
  59.         {
  60.             DeleteMenuItem(menuRef,iQuit);
  61.             DeleteMenuItem(menuRef,iQuit - 1);
  62.             DisableMenuItem(menuRef,0);
  63.         }
  64.     }
  65.     else
  66.     {
  67.         menuRef = GetMenuRef(mFile);
  68.         if(menuRef != NULL)
  69.             SetMenuItemCommandID(menuRef,iQuit,kHICommandQuit);
  70.     }
  71.  
  72.     // ……………………………………………………………………………………………………………………… install application event handler and timer
  73.  
  74.     InstallApplicationEventHandler(NewEventHandlerUPP((EventHandlerProcPtr) appEventHandler),
  75.                                                                  GetEventTypeCount(applicationEvents),applicationEvents,
  76.                                                                  0,NULL);
  77.  
  78.     InstallEventLoopTimer(GetCurrentEventLoop(),0,1,
  79.                                                 NewEventLoopTimerUPP((EventLoopTimerProcPtr) doIdle),NULL,NULL);
  80.  
  81.     // …………………………………………………………………………………………………………………………………………………………………………………………………………… open window
  82.  
  83.     CreateNewWindow(kDocumentWindowClass,kWindowStandardHandlerAttribute |
  84.                                     kWindowStandardDocumentAttributes,&contentRect,&gWindowRef);
  85.  
  86.     ChangeWindowAttributes(gWindowRef,0,kWindowCloseBoxAttribute);
  87.     SetWTitle(gWindowRef,"\pMiscellany");
  88.     RepositionWindow(gWindowRef,NULL,kWindowAlertPositionOnMainScreen);
  89.  
  90.     SetPortWindowPort(gWindowRef);
  91.     TextSize(10);
  92.  
  93.     ShowWindow(gWindowRef);
  94.     GetWindowPortBounds(gWindowRef,&portRect);
  95.     InvalWindowRect(gWindowRef,&portRect);
  96.  
  97.     // ……………………………………………………………………………………………………………………………………………………………… install window event handler
  98.  
  99.     InstallWindowEventHandler(gWindowRef,
  100.                                                         NewEventHandlerUPP((EventHandlerProcPtr) windowEventHandler),
  101.                                                         GetEventTypeCount(windowEvents),windowEvents,0,NULL);
  102.  
  103.     // ……………………………………………………………………………………………………………………………………………………………… create control and help tags
  104.  
  105.     CreateBevelButtonControl(gWindowRef,&controlRect,CFSTR("Control"),
  106.                                                      kControlBevelButtonNormalBevel,kControlBehaviorPushbutton,
  107.                                                      NULL,0,0,0,&gBevelButtonControlRef);
  108.     doHelpTagControl();
  109.     HideControl(gBevelButtonControlRef);
  110.     doHelpTagWindow();
  111.     HMSetHelpTagsDisplayed(false);
  112.     
  113.     // …………………………………………………………………………………………………………………………… get process serial number of this process
  114.  
  115.     GetCurrentProcess(&gProcessSerNum);    
  116.  
  117.     // …………………………………………………………………………………………………………………………………………………………………… run application event loop
  118.  
  119.     RunApplicationEventLoop();
  120. }
  121.  
  122. // *************************************************************************** doPreliminaries
  123.  
  124. void  doPreliminaries(void)
  125. {
  126.     MoreMasterPointers(640);
  127.     InitCursor();
  128. }
  129.  
  130. // *************************************************************************** appEventHandler
  131.  
  132. OSStatus  appEventHandler(EventHandlerCallRef eventHandlerCallRef,EventRef eventRef,
  133.                                                     void * userData)
  134. {
  135.     OSStatus            result = eventNotHandledErr;
  136.     UInt32                eventClass;
  137.     UInt32                eventKind;
  138.     HICommand            hiCommand;
  139.     MenuID                menuID;
  140.     MenuItemIndex    menuItem;
  141.  
  142.     eventClass = GetEventClass(eventRef);
  143.     eventKind  = GetEventKind(eventRef);
  144.  
  145.     switch(eventClass)
  146.     {
  147.         case kEventClassApplication:
  148.             if(eventKind == kEventAppActivated)
  149.             {
  150.                 if(gNotificationInQueue)
  151.                     doDisplayMessageToUser();
  152.                 result = noErr;
  153.             }
  154.             break;
  155.  
  156.         case kEventClassCommand:
  157.             if(eventKind == kEventProcessCommand)
  158.             {
  159.                 GetEventParameter(eventRef,kEventParamDirectObject,typeHICommand,NULL,
  160.                                                     sizeof(HICommand),NULL,&hiCommand);
  161.                 menuID = GetMenuID(hiCommand.menu.menuRef);
  162.                 menuItem = hiCommand.menu.menuItemIndex;
  163.                 if((hiCommand.commandID != kHICommandQuit) && 
  164.                      (menuID >= mAppleApplication && menuID <= mDemonstration))
  165.                 {
  166.                     doMenuChoice(menuID,menuItem);
  167.                     result = noErr;
  168.                 }
  169.             }
  170.             break;
  171.     }
  172.  
  173.     return result;
  174. }
  175.  
  176. // ************************************************************************ windowEventHandler
  177.  
  178. OSStatus  windowEventHandler(EventHandlerCallRef eventHandlerCallRef,EventRef eventRef,
  179.                                                          void* userData)
  180. {
  181.     OSStatus    result = eventNotHandledErr;
  182.     UInt32        eventClass;
  183.     UInt32        eventKind;
  184.     WindowRef    windowRef;
  185.     SInt32        deviceLoopUserData;
  186.     RgnHandle    regionHdl;
  187.     Rect            portRect, positioningBounds;
  188.     Point            idealHeightAndWidth, minimumHeightAndWidth;
  189.  
  190.     eventClass = GetEventClass(eventRef);
  191.     eventKind  = GetEventKind(eventRef);
  192.  
  193.     switch(eventClass)
  194.     {
  195.         case kEventClassWindow:
  196.             GetEventParameter(eventRef,kEventParamDirectObject,typeWindowRef,NULL,sizeof(windowRef),
  197.                                                 NULL,&windowRef);
  198.             switch(eventKind)
  199.             {
  200.                 case kEventWindowDrawContent:
  201.                     if(gMultiMonitorsDrawDemo)
  202.                     {
  203.                         RGBBackColor(&gWhiteColour);
  204.                         deviceLoopUserData = (SInt32) windowRef;
  205.                         regionHdl = NewRgn();
  206.                         if(regionHdl)
  207.                         {
  208.                             GetPortVisibleRegion(GetWindowPort(windowRef),regionHdl);
  209.                             DeviceLoop(regionHdl,gDeviceLoopDrawUPP,deviceLoopUserData,0);
  210.                             DisposeRgn(regionHdl);
  211.                         }
  212.                     }
  213.                     else if(gColourPickerDemo )
  214.                     {
  215.                         RGBBackColor(&gBlueColour);
  216.                         GetWindowPortBounds(windowRef,&portRect);
  217.                         EraseRect(&portRect);
  218.                         doDrawColourPickerChoice();
  219.                     }
  220.                     else
  221.                     {
  222.                         RGBBackColor(&gBlueColour);
  223.                         GetWindowPortBounds(windowRef,&portRect);
  224.                         EraseRect(&portRect);
  225.                         if(gHelpTagsDemo)
  226.                         {
  227.                             Draw1Control(gBevelButtonControlRef);
  228.                             RGBForeColor(&gWhiteColour);
  229.                             MoveTo(10,20);
  230.                             DrawString("\pHover the cursor in the window, and over the bevel button, ");
  231.                             DrawString("\puntil the Help tag appears.");
  232.                             MoveTo(10,35);
  233.                             DrawString("\pPress the Command key to invoke the maximum content.");
  234.                             MoveTo(10,50);
  235.                             DrawString("\pDrag the window to a new location.");
  236.                         }
  237.                     }
  238.                     result = noErr;
  239.                     break;
  240.  
  241.                 case kEventWindowGetIdealSize:
  242.                     GetAvailableWindowPositioningBounds(GetMainDevice(),&positioningBounds);
  243.                     idealHeightAndWidth.v = positioningBounds.bottom;
  244.                     idealHeightAndWidth.h = positioningBounds.right;
  245.                     SetEventParameter(eventRef,kEventParamDimensions,typeQDPoint,
  246.                                                         sizeof(idealHeightAndWidth),&idealHeightAndWidth);
  247.                      result = noErr;
  248.                     break;
  249.  
  250.                 case kEventWindowGetMinimumSize:
  251.                     minimumHeightAndWidth.v = 302; 
  252.                     minimumHeightAndWidth.h = 445;
  253.                     SetEventParameter(eventRef,kEventParamDimensions,typeQDPoint,
  254.                                                         sizeof(minimumHeightAndWidth),&minimumHeightAndWidth);
  255.                     result = noErr;
  256.                     break;
  257.  
  258.                 case kEventWindowBoundsChanged:
  259.                     doHelpTagWindow();
  260.                     GetWindowPortBounds(windowRef,&portRect);
  261.                     InvalWindowRect(windowRef,&portRect);
  262.                     result = noErr;
  263.                     break;
  264.             }
  265.             break;
  266.     }
  267.  
  268.     return result;
  269. }
  270.  
  271. // ****************************************************************************** doMenuChoice
  272.  
  273. void  doMenuChoice(MenuID menuID,MenuItemIndex menuItem)
  274. {
  275.     Rect    portRect;
  276.  
  277.     if(menuID == 0)
  278.         return;
  279.  
  280.     switch(menuID)
  281.     {
  282.         case mAppleApplication:
  283.             if(menuItem == iAbout)
  284.                 SysBeep(10);
  285.             break;
  286.  
  287.         case mDemonstration:
  288.             gMultiMonitorsDrawDemo = gColourPickerDemo = gHelpTagsDemo = false;
  289.             if(HMAreHelpTagsDisplayed)
  290.                 HMSetHelpTagsDisplayed(false);
  291.             HideControl(gBevelButtonControlRef);
  292.             GetWindowPortBounds(gWindowRef,&portRect);
  293.  
  294.             switch(menuItem)
  295.             {
  296.                 case iNotification:
  297.                     RGBBackColor(&gBlueColour);
  298.                     EraseRect(&portRect);
  299.                     doSetUpNotification();
  300.                     break;
  301.                     
  302.                 case iProgress:
  303.                     RGBBackColor(&gBlueColour);
  304.                     EraseRect(&portRect);
  305.                     doProgressBar();
  306.                     break;
  307.  
  308.                 case iColourPicker:
  309.                     gColourPickerDemo = true;
  310.                     doColourPicker();
  311.                     break;
  312.                     
  313.                 case iMultiMonitors:
  314.                     gMultiMonitorsDrawDemo = true;
  315.                     InvalWindowRect(gWindowRef,&portRect);
  316.                     break;
  317.  
  318.                 case iHelpTag:
  319.                     gHelpTagsDemo = true;
  320.                     InvalWindowRect(gWindowRef,&portRect);
  321.                     ShowControl(gBevelButtonControlRef);
  322.                     HMSetHelpTagsDisplayed(true);
  323.                     break;
  324.             }
  325.  
  326.             break;
  327.     }
  328. }
  329.  
  330. // *******************************************************************************************